/*
 * @Author: your name
 * @Date: 2021-04-15 10:28:31
 * @LastEditTime: 2021-07-09 10:46:33
 * @LastEditors: Please set LastEditors
 * @Description:分页混入
 * @FilePath: \future\common\mixin\page.js
 */
// 使用须知
// 获取数据方法命名为 getDataList
// 数据集合命名为 dataList
//提交表单命名为  subForm   参数 page 默认值要设置为1
//getDataList获取数据成功 必须return { length,data}  length数据长度  data得到的数据
//首次加载数据需要传入 参数 frist  判断是否赋值给 getDataList获取数据成功
//接口参数 req_param
export default {
    data() {
        return {
            status: "loadmore", // 加载前值为loadmore，加载中为loading，没有数据为nomore
            loading: true, //加载状态
            dataList: [], //数据
            dataTotal: "--", //数据总条数
        };
    },
    // 页面触底触发
    async onReachBottom() {
        console.log("页面触底 :>> 分页触发", this.subForm, this.status);
        if (this.status == "nomore" || this.status == "loading") return;
        this.subForm.page++;
        this.status = "loading";
        const { length, data } = await this.getDataList(this.req_param);

        this.dataList = this.dataList.concat(data);

        await this.pageCallBack(data); //获取数据后触发回调

        if (length < (this.subForm.per_page ? this.subForm.per_page : 10)) {
            this.status = "nomore";
            return;
        }
        this.status = "loadmore";
    },
    methods: {
        /*  // 获取当前页码,多次自动触发触底函数，直到当达到当前页面数   用于页面返回刷新数据 backNav()
        async goToPage(page) {
            console.log('page :>> ', page);
            this.subForm.page=1
            for (let i = 0; i < page; i++) {
                if (i == 0) {
                    this.getDataList(this.req_param);
                } else {
                    console.log('i :>> ', i);
                    if (this.status == "nomore" || this.status == "loading") return;
                    this.subForm.page++;
                    this.status = "loading";
                    const { length, data } = await this.getDataList(this.req_param);
                    this.dataList = this.dataList.concat(data);
                    if (length < (this.subForm.per_page ? this.subForm.per_page : 10)) {
                        this.status = "nomore";
                        return;
                    }
                    this.status = "loadmore";
                }
            }
        }, */
        // 获取页面数据回调
        async pageCallBack(data) {},
        //判断是否是json格式
        isJSON(str) {
            try {
                let obj = JSON.parse(str);
                return !!obj && typeof obj === "object";
            } catch (e) {}
            return false;
        },
        // scroll-view触底
        async scrolltolower() {
            console.log("scroll-view触底 :>> 分页触发", this.subForm, this.status);
            if (this.status == "nomore" || this.status == "loading") return;
            this.subForm.page++;
            this.status = "loading";
            const { length, data } = await this.getDataList(this.req_param);

            this.dataList = this.dataList.concat(data);

            await this.pageCallBack(data); //获取数据后触发回调
            
            if (length < (this.subForm.per_page ? this.subForm.per_page : 10)) {
                this.status = "nomore";
                return;
            }
            this.status = "loadmore";
        },
        /**
         * @description: 记录列表
         *  @param {String} api_list_name 接口所属集合名字
         *  @param {String} api_name 接口名字
         * @param {fun} fun 处理data数据回调
         * @return {Obj}
         */
        async getDataList(param, fun) {
            if(!this.isLogin) {
                return;
            }
            const { api_list_name, api_name } = param;
            const AppCode = uni.getStorageSync("AppCode");
            //若参数是数组则转json
            Object.keys(this.subForm).forEach((key) => {
                this.subForm[key] = this.subForm[key] instanceof Array ? JSON.stringify(this.subForm[key]) : this.subForm[key];
            });
            const { code, data, total } = await this.$api[AppCode][api_list_name][api_name](this.subForm);
            if (code == 200) {
                if (this.subForm.page == 1) {
                    this.dataTotal = total;
                    this.status = data.length < (this.subForm.per_page ? this.subForm.per_page : 10) ? "nomore" : "loadmore";
                    
                    this.dataList = data;
                    fun && fun(data);
                    await this.pageCallBack(data); //获取数据后触发回调
                    
                }
                
                
                this.loading = false;
                //若参数是json则转数组
                Object.keys(this.subForm).forEach((key) => {
                    this.subForm[key] = this.isJSON(this.subForm[key]) ? JSON.parse(this.subForm[key]) : this.subForm[key];
                });
                return {
                    length: data.length,
                    data,
                };
            }
        },
    },
};
